package rs.etf.kdp.client;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class ChangeClientName extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3456267266434238134L;
	private final JPanel contentPanel = new JPanel();
	private JTextField txtNewName;
	private Properties data;
	private JLabel lblOldClientName;
	private JFrame parent;
	private ClientGUI cgui;


	/**
	 * Create the dialog.
	 */
	public ChangeClientName(JFrame frame, ClientGUI clientGUI) {
		parent = frame;
		cgui = clientGUI;
		setTitle("Change Client Name");
		setBounds(100, 100, 401, 193);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		{
			JPanel pnlOldName = new JPanel();
			contentPanel.add(pnlOldName, BorderLayout.NORTH);
			{
				JLabel lblOldClientName = new JLabel("Old Client Name:");
				pnlOldName.add(lblOldClientName);
			}
			{
				lblOldClientName = new JLabel("New label");
				pnlOldName.add(lblOldClientName);
			}
		}
		{
			JPanel pnlNewName = new JPanel();
			contentPanel.add(pnlNewName, BorderLayout.CENTER);
			pnlNewName.setLayout(new BorderLayout(0, 0));
			{
				JLabel lblEnterNewClient = new JLabel("Enter new client name:\r\n");
				pnlNewName.add(lblEnterNewClient, BorderLayout.NORTH);
			}
			{
				txtNewName = new JTextField();
				pnlNewName.add(txtNewName, BorderLayout.CENTER);
				txtNewName.setColumns(10);
			}
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						setVisible(false);
						cgui.fillInfo();
						
					}
				});
				{
					JButton btnSave = new JButton("Save");
					btnSave.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent arg0) {
							try {
								saveProperty();
							} catch (FileNotFoundException e) {
								JOptionPane.showMessageDialog(parent, "Unable to store property file ","Error",JOptionPane.ERROR_MESSAGE);
							} catch (IOException e) {
								JOptionPane.showMessageDialog(parent, "Error occured while saving file","Error",JOptionPane.ERROR_MESSAGE);
							}
						}
					});
					buttonPane.add(btnSave);
				}
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
		}
		loadProp();
	}
	
	private void loadProp()
	{
		data = new Properties();
		try {
			data.load(new FileInputStream("res/data.properties"));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			lblOldClientName.setText("Unable to load property file");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		String oldname = data.getProperty("name");
		lblOldClientName.setText(oldname);
		
	}
	
	private void saveProperty() throws FileNotFoundException, IOException
	{
		String newName = txtNewName.getText();
		data.setProperty("name", newName);
		data.store(new FileOutputStream("res/data.properties"), null);
		JOptionPane.showMessageDialog(this, "You have successfully changed client name");
	}

}
